﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace DALL.Progect
{
    public class T_PUB_PROJECTFILE
    {
        public T_PUB_PROJECTFILE()
        { }
        /// <summary>
        /// 获取文件下的项目
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetProjectList(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT p.PROJECT_ID, \n");
            varname1.Append("       p.PROJECT_NAME, \n");
            varname1.Append("       p.PRICE, \n");
            varname1.Append("       p.PROJECTTYPE_ID, \n");
            varname1.Append("       p.BUDGET, \n");
            varname1.Append("       p.REMARK, \n");
            varname1.Append("       f.ID \n");
            varname1.Append("FROM   T_PUB_PROJECTFILE f, \n");
            varname1.Append("       T_PUB_PROJECT p \n");
            varname1.Append("WHERE  p.PROJECT_ID = f.PROJECT_ID ");
            if (strWhere.Trim() != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());
        }
        //获得图片
        public DataTable GetProjectImageUrl(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT f.URLThumbnail, \n");
            varname1.Append("       f.URLOriginal, \n");
            varname1.Append("       pf.PROJECT_ID,f.URLVideoPhoto, \n");
            varname1.Append("       f.TYPE \n");
            varname1.Append("FROM   T_PUB_PROJECTFILE pf \n");
            varname1.Append("       LEFT JOIN T_PUB_FILE f \n");
            varname1.Append("         ON pf.FILE_ID = f.FILE_ID ");
            if (strWhere != "")
            {
                varname1.Append(strWhere);
            }
            varname1.Append(" order by f.URLThumbnail desc,f.TYPE");
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString()).Tables[0];
        }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_PUB_PROJECTFILE where ID=@ID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.Progect.T_PUB_PROJECTFILE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_PROJECTFILE(");
            strSql.Append("PROJECT_ID,FILE_ID,ORDER_INDEX)");

            strSql.Append(" values (");
            strSql.Append("@PROJECT_ID,@FILE_ID,@ORDER_INDEX)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, model.PROJECT_ID);
            db.AddInParameter(dbCommand, "FILE_ID", DbType.String, model.FILE_ID);
            db.AddInParameter(dbCommand, "ORDER_INDEX", DbType.Int32, model.ORDER_INDEX);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Progect.T_PUB_PROJECTFILE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PROJECTFILE set ");
            strSql.Append("PROJECT_ID=@PROJECT_ID,");
            strSql.Append("FILE_ID=@FILE_ID,");
            strSql.Append("ORDER_INDEX=@ORDER_INDEX");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "PROJECT_ID", DbType.String, model.PROJECT_ID);
            db.AddInParameter(dbCommand, "FILE_ID", DbType.String, model.FILE_ID);
            db.AddInParameter(dbCommand, "ORDER_INDEX", DbType.Int32, model.ORDER_INDEX);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_PROJECTFILE ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
     
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Progect.T_PUB_PROJECTFILE GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROJECT_ID,FILE_ID from T_PUB_PROJECTFILE,ORDER_INDEX ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.Progect.T_PUB_PROJECTFILE model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROJECT_ID,FILE_ID,ORDER_INDEX ");
            strSql.Append(" FROM T_PUB_PROJECTFILE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Progect.T_PUB_PROJECTFILE> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,PROJECT_ID,FILE_ID,ORDER_INDEX ");
            strSql.Append(" FROM T_PUB_PROJECTFILE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Progect.T_PUB_PROJECTFILE> list = new List<Model.Progect.T_PUB_PROJECTFILE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Progect.T_PUB_PROJECTFILE ReaderBind(IDataReader dataReader)
        {
            Model.Progect.T_PUB_PROJECTFILE model = new Model.Progect.T_PUB_PROJECTFILE();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            model.PROJECT_ID = dataReader["PROJECT_ID"].ToString();
            model.FILE_ID = dataReader["FILE_ID"].ToString();
            ojb = dataReader["ORDER_INDEX"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ORDER_INDEX = (int)ojb;
            }
            return model;
        }

        #endregion  Method
    }
}
